from django.contrib.auth.models import Group as Project
from django.contrib.auth.models import User

from django.db.models import (Model, CharField, ForeignKey, IntegerField, BooleanField,
                              TextField, OneToOneField,
                              ImageField, FileField, DateTimeField)

class Document(Model):
   project = ForeignKey(
      Project,
      help_text = 'Each document is part of a project')
   name = CharField(
      max_length = 64,
      help_text = 'Each document has a unique name within a project')
   prefix = CharField(
      max_length=8,
      help_text = 'Each document has a unique short prefix wihin a project.'\
      'This is to identify requirements created in this document')
   active = BooleanField(
      default = True,
      help_text = 'Documents never get deleted they just go inactive')
   writeable = BooleanField(
      default = True,
      help_text = 'Writeable documents can be updated, otherwise they are frozen')
   class Meta:
      unique_together = ('project', 'name'), ('project', 'prefix')
      ordering = 'project',
   def __unicode__(self):
      return u'%s/%s[%s]' % (self.project, self.name, self.prefix)

class Requirement(Model):
   document = ForeignKey(
      Document,
      help_text='Each requirement is part of a document')
   order = IntegerField(
      help_text='Each requirement has an order within a document')
   user = ForeignKey(
      User,
      help_text='Record of last user to update the requirement')
   time = DateTimeField(
      auto_now = True,
      help_text='The last time the requirement was modified')
   active = BooleanField(
      default=True,
      help_text='Requirements never get deleted they just go inactive')
   writeable = BooleanField(
      default=True,
      help_text='Writeable requirements can be updated, otherwise they are frozen')
   class Meta:
      unique_together = 'document', 'order'
      ordering = 'document',
   @property
   def unique(self):
      return u'%s_%d' % (self.document.prefix, self.pk)
   @property
   def parts(self):
      result=[]
      if hasattr(self,'section'):result.append('section')
      if hasattr(self, 'multilinetext'):result.append('multi')
      if hasattr(self, 'onelinetext'):result.append('oneline')
      if hasattr(self, 'table'):result.append('table')
      if hasattr(self, 'alias'):result.append('alias')
      if hasattr(self, 'image'):result.append('image')
      if hasattr(self, 'file'):result.append('file')
      return ','.join(result)
   def __unicode__(self):
      return u'%04d:%s' % (self.order, self.unique)

class Section(Model):
   requirement = OneToOneField(Requirement, primary_key=True)
   kind = CharField(max_length=1, choices=[('%d' % i, 'Head%d' % i) for i in range(10)])
   text = CharField(max_length=64)
   class Meta:
      ordering = 'requirement',
   @property
   def project(self):
      return str(self.requirement.document.project)
   @property
   def document(self):
      return str(self.requirement.document)
   def __unicode__(self):
      return u'Section[%s %s %s]' % (self.requirement, self.kind, self.text)

class MultiLineText(Model):
   requirement = OneToOneField(Requirement, primary_key=True)
   text = TextField()
   class Meta:
      ordering = 'requirement',
   @property
   def project(self):
      return str(self.requirement.document.project)
   @property
   def document(self):
      return str(self.requirement.document)
   def __unicode__(self):
      return u'MultiLine[%s %s...]' % (self.requirement, self.text.split('\n')[0])

class Table(Model):
   requirement = OneToOneField(Requirement, primary_key=True)
   text = TextField(help_text='comma separated headings plus data (escape comma and backslash)')
   class Meta:
      ordering = 'requirement',
   @property
   def project(self):
      return str(self.requirement.document.project)
   @property
   def document(self):
      return str(self.requirement.document)
   def __unicode__(self):
      return u'Table[%s %s...]' % (self.requirement, self.text.split('\n')[0])

class OneLineText(Model):
   requirement = OneToOneField(Requirement, primary_key=True)
   text = CharField(max_length=128)
   class Meta:
      ordering = 'requirement',
   @property
   def project(self):
      return str(self.requirement.document.project)
   @property
   def document(self):
      return str(self.requirement.document)
   def __unicode__(self):
      return u'OneLine[%s %s]' % (self.requirement, self.text)

class Alias(Model):
   requirement = OneToOneField(Requirement, primary_key=True)
   alias = ForeignKey(Requirement, related_name='alias_ref')
   class Meta:
      ordering = 'requirement',
   @property
   def project(self):
      return str(self.requirement.document.project)
   @property
   def document(self):
      return str(self.requirement.document)
   def __unicode__(self):
      return u'Alias[%s %s]' % (self.requirement, self.alias)

class Image(Model):
   requirement = OneToOneField(Requirement, primary_key=True)
   image = ImageField(upload_to='.')
   class Meta:
      ordering = 'requirement',
   @property
   def project(self):
      return str(self.requirement.document.project)
   @property
   def document(self):
      return str(self.requirement.document)
   def __unicode__(self):
      return u'Image[%s %s]' % (self.requirement, self.image)

class File(Model):
   requirement = OneToOneField(Requirement, primary_key=True)
   file = FileField(upload_to='.')
   class Meta:
      ordering = 'requirement',
   @property
   def project(self):
      return str(self.requirement.document.project)
   @property
   def document(self):
      return str(self.requirement.document)
   def __unicode__(self):
      return u'File[%s %s]' % (self.requirement, self.file)

class Archive(Model):
   project = ForeignKey(Project)
   name = CharField(max_length=64)
   time = DateTimeField(auto_now=True)
   class Meta:
      unique_together = 'project', 'name'
      ordering = 'time',
   def __unicode__(self):
      return u'Archive[%s %s]' % (self.name, self.time)
